package com.dataseek.iot.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.device.entity.Question;
import com.dataseek.iot.device.entity.QuestionTypeNum;
import com.dataseek.iot.device.mapper.QuestionMapper;
import com.dataseek.iot.device.service.QuestionService;
import com.dataseek.iot.device.vo.QuestionVo;
import com.dataseek.iot.toolikit.common.util.DateUtils;
import com.dataseek.iot.toolkit.auth.entity.SysUser;
import com.dataseek.iot.toolkit.auth.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {


    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public QuestionVo queryAction(String userId, String status, String title, Integer num, Integer size) {

        QuestionVo lineVo = new QuestionVo();
        QueryWrapper<Question> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(userId)) {
            qw.eq("user_id", userId);
        }

        if (!StringUtils.isEmpty(title)) {
            qw.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            qw.eq("status", status);
        }
        if (!StringUtils.isEmpty(status)&&status.equals("0")) {//待解决列表默认根据创建时间倒序
            qw.orderByDesc("create_time");
        }else if (!StringUtils.isEmpty(status)&&status.equals("2")) {//已解决列表默认根据更新时间倒序
            qw.orderByDesc("update_time");
        }

        IPage<Question> page = new Page<>(num, size);
        List<Question> mapList = questionMapper.selectPage(page, qw).getRecords();
        lineVo.setPage(num);
        lineVo.setSize(size);
        lineVo.setTotal(page.getTotal());

        //获取userName
        QueryWrapper<SysUser> qwUser = new QueryWrapper<>();
        for (Question question : mapList) {
            qwUser.eq("user_id", Integer.parseInt(question.getUserId()));
            SysUser tmpUser = sysUserMapper.selectOne(qwUser);
            if (tmpUser != null) {
                question.setUserName(tmpUser.getUserName());
            }
        }

        lineVo.setList(mapList);
        return lineVo;
    }

    @Override
    public Question qryById(Long id) {
        return questionMapper.selectById(id);
    }

    @Override
    public Integer insertAction(Question Question) {
        this.saveOrUpdate(Question);
        return 0;

    }

    @Override
    public Integer updateAction(Question Question) {
        int resultCode = questionMapper.updateById(Question);
        return resultCode;
    }

    @Override
    public List<QuestionTypeNum> queryTypeNum() {
        List<QuestionTypeNum> reList = new ArrayList<>();
        QueryWrapper<Question> qw = new QueryWrapper<>();
        qw.ge("create_time", this.getFirstDayOfMonth());
        List<Question> queList = this.list(qw);
        if (!CollectionUtils.isEmpty(queList)) {
            Map<String, List<Question>> queMap = queList.stream().collect(Collectors.groupingBy(Question::getType));
            for (Map.Entry<String, List<Question>> en : queMap.entrySet()) {
                QuestionTypeNum typeDto = new QuestionTypeNum();
                typeDto.setTypeName(this.transType(en.getKey()));
                typeDto.setCnt(en.getValue().size());
                reList.add(typeDto);
            }
        }
        return reList;
    }

    private String transType(String type) {
        String typeName;
        switch (type) {
            case "01":
                typeName = "运营问题";
                break;
            case "02":
                typeName = "设备报修";
                break;
            case "03":
                typeName = "设备异常";
                break;
            case "04":
                typeName = "储液罐相关问题";
                break;
            case "05":
                typeName = "程序闪退";
                break;
            default:
                typeName = "其他";
        }
        return typeName;
    }


    //获取每月第一天
    private Date getFirstDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return  calendar.getTime();
    }
}
